import {
  getInboundDetail,
  getInboundDetailMaterial,
  inboundList,
  inStoreTypePageSelect,
  rejectInbound,
  saveinboundCheck,
} from '@/api/store'
import moment from 'moment'
import { ref } from 'vue'

/**
 * 入库表单
 */
export const useWarehouseEntryForm = () => {
  // 表单
  const form = ref<StorageAPIRequest.WarehouseEntrySave>({
    createTime: moment().format('YYYY-MM-DD HH:mm:SS'),
    delInboundMaterialIds: [],
    estimateTime: '',
    id: '',
    inboundMaterialVos: [],
    remark: '',
    reviewRemark: '',
    source: 1,
    storeType: 0,
    typeId: '',
  })

  // 驳回入库单表单
  const rejectForm = ref({
    remark: '',
  })

  // 保存表单加载
  const saveLoading = ref(false)

  // 驳回入库单加载
  const rejectLoading = ref(false)

  /**
   * 保存
   */
  const add = async () => {
    saveLoading.value = true
    const res = await saveinboundCheck(form.value, 1)
    saveLoading.value = false
    return res.data
  }

  /**
   * 审核
   */
  const audit = async () => {
    saveLoading.value = true
    const res = await saveinboundCheck(form.value, 2)
    saveLoading.value = false
    return res.data
  }

  /**
   * 重置表单
   */
  const resetForm = () => {
    form.value = {
      createTime: moment().format('YYYY-MM-DD HH:mm:SS'),
      delInboundMaterialIds: [],
      estimateTime: '',
      id: '',
      inboundMaterialVos: [],
      remark: '',
      reviewRemark: '',
      source: 1,
      storeType: 0,
      typeId: '',
    }
  }

  /**
   * 驳回入库单
   * @param {String} id - 入库单id
   */
  const reject = async (id: string) => {
    rejectLoading.value = true
    const res = await rejectInbound(rejectForm.value, id)
    rejectLoading.value = false
    return res.data
  }

  return {
    form,
    rejectForm,
    saveLoading,
    rejectLoading,
    add,
    audit,
    reject,
    resetForm,
  }
}

/**
 * 获取入库详情
 */
export const useGetWarehouseEntryDetail = () => {
  // 获取详情加载
  const getDetailLoading = ref(false)

  // 入库单详情
  const detail = ref<WarehouseEntryDetail>({
    archivedEmp: '',
    archivedRemark: '',
    archivedTime: '',
    cancelEmp: '',
    cancelRemark: '',
    cancelTime: '',
    createEmp: '',
    createTime: '',
    estimateTime: '',
    id: '',
    inboundEmp: '',
    inboundNo: '',
    inboundRemark: '',
    inboundTime: '',
    logisticsName: '',
    logisticsNo: '',
    logisticsRemark: '',
    rejectEmp: '',
    rejectRemark: '',
    rejectTime: '',
    remark: '',
    reviewEmp: '',
    reviewRemark: '',
    reviewTime: '',
    source: 1,
    status: 1,
    storeType: 0,
    type: '',
    typeId: '',
    updateEmp: '',
    updateTime: '',
  })

  /**
   * 获取详情
   * @param {String} id - 入库id
   */
  const getDetail = async (id: string) => {
    getDetailLoading.value = true
    const res = await getInboundDetail(id)
    detail.value = res.data.data.row
    getDetailLoading.value = false
  }

  return {
    detail,
    getDetailLoading,
    getDetail,
  }
}

/**
 * 获取入库详情的产品列表
 */
export const useGetWarehouseEntryDetailGoodList = () => {
  // 获取产品列表加载
  const getGoodListLoading = ref(false)

  // 产品列表
  const goodList = ref<Array<WarehouseEntryDetailGood>>([])

  /**
   * 获取产品列表
   * @param {String} id - 入库id
   */
  const getGoodList = async (id: string) => {
    getGoodListLoading.value = true
    const res = await getInboundDetailMaterial(id)
    goodList.value = res.data.data.row
    getGoodListLoading.value = false
  }

  return {
    goodList,
    getGoodListLoading,
    getGoodList,
  }
}

/**
 * 获取入库列表
 */
export const useGetWarehouseInList = () => {
  // 获取列表加载
  const getListLoading = ref(false)

  // 获取列表请求参数
  const getListParam = ref<StorageAPIRequest.GetWarehouseInListParam>({
    dateType: null,
    endTime: '',
    limit: 50,
    page: 1,
    search: '',
    source: null,
    startTime: '',
    status: null,
    typeId: null,
  })

  // 列表
  const list = ref<Array<WarehouseInListItem>>([])

  // 列表总条数
  const listTotal = ref(0)

  /**
   * 获取列表
   */
  const getList = async () => {
    getListLoading.value = true
    const res = await inboundList(getListParam.value)
    list.value = res.data.data.rows
    listTotal.value = Number(res.data.data.total)
    getListLoading.value = false
  }

  /**
   * 重置搜索条件
   */
  const resetGetListParam = () => {
    getListParam.value = {
      dateType: null,
      endTime: '',
      limit: 50,
      page: 1,
      search: '',
      source: null,
      startTime: '',
      status: null,
      typeId: null,
    }
  }

  return {
    list,
    listTotal,
    getListParam,
    getListLoading,
    getList,
    resetGetListParam,
  }
}

/**
 * 获取入库类型下拉选项
 */
export const useGetWarehouseInOptions = () => {
  // 获取选项加载
  const getOptionsLoading = ref(false)

  // 获取选项请求参数
  const getOptionsParam = ref({
    limit: 50,
    page: 1,
    name: '',
  })

  // 类型下拉选项
  const options = ref<Array<WarehouseEntryDocumentTypeOptions>>([])

  /**
   * 获取选项
   */
  const getOptions = async () => {
    getOptionsLoading.value = true
    const res = await inStoreTypePageSelect(getOptionsParam.value)
    getOptionsLoading.value = false
    options.value = res.data.data.rows
  }

  return {
    options,
    getOptionsParam,
    getOptionsLoading,
    getOptions,
  }
}
